/*
 *
 *  Copyright (C) 2009 GSyC/LibreSoft
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author : Roberto Calvo Palomino <rocapal@gsyc.es>
 *
 */

package com.LibreGeoSocial.App.Routes;

import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.util.Log;

import com.LibreGeoSocial.Api.GeoNode;

public class GoogleRoutes {
	
	private String mSource;
	private String mDestination;
	private String mLanguage;
	
	//FIXME take key from strings.xml
	private static String GOOGLE_API_KEY = "05WTwe1iYd6AAhWEI2Wzjqrxnu2Vv1Z9jkqnjYA";
	
	ArrayList<GeoNode> mPlaces = new ArrayList<GeoNode>();
	
	ArrayList<GeoNode> mRoutePath = new ArrayList<GeoNode>();
	
	public GoogleRoutes ()
	{
		mSource = null;
		mDestination = null;
		mLanguage = "es";
	}

	public void setSource (String source)
	{
		mSource = source;
	}	
	
	public void setSource (Double latitude, Double longitude)
	{
		mSource = latitude.toString() + "," + longitude.toString();
	}
	
	public void setDestination (String destination)
	{
		mDestination = destination;
	}
	
	public void setDestination (Double latitude, Double longitude)
	{
		mDestination = latitude.toString() + "," + longitude.toString();
	}
	
	public void setLanguage (String language)
	{
	
		if ( language.compareTo("es") == 0 || language.compareTo("en") == 0)
			mLanguage = language;
		else
			mLanguage = "es";			
		
	}
	
	public ArrayList<GeoNode> getRoutePath ()
	{
		return mRoutePath;
	}
	
    public ArrayList<GeoNode> getPlaces ()
    {
    	return mPlaces;
    }
	
	public ArrayList<GeoNode> getRoute ()
	{
		
		// http://maps.google.com/maps?q=from:Madrid+to:Barcelona&key=0Tp7WsIDiyqp_gJLEiO55RXtkz7VsxOjQvL44SA&output=kml&sensor=true&oe=utf8&hl=es
		
		try { 
			
			
			URL url = new URL("http://maps.google.com/maps?q=from:" + mSource + 
													"+to:" + mDestination + 
													"&key=" + GOOGLE_API_KEY + 
													"&output=kml&sensor=true&oe=utf8&hl=" + mLanguage);

			 
			Log.e("GoogleRoutes",url.toString());
	        /* Get a SAXParser from the SAXPArserFactory. */
	        SAXParserFactory spf = SAXParserFactory.newInstance();
	        SAXParser sp = spf.newSAXParser(); 
	        
	        /* Get the XMLReader of the SAXParser we created. */
            XMLReader xr = sp.getXMLReader();
            /* Create a new ContentHandler and apply it to the XML-Reader*/
            GoogleRoutesHandler myExampleHandler = new GoogleRoutesHandler();
            xr.setContentHandler(myExampleHandler); 
            
            /* Parse the xml-data from our URL. */
            xr.parse(new InputSource(url.openStream()));
            
            mPlaces = myExampleHandler.getPlaces();
            
            mRoutePath = myExampleHandler.getRoutePath();
    		
            return mPlaces;
        
        } catch (Exception e) {
            /* Display any Error to the GUI. */            
        	Log.e("GoogleRoutes", "getRoute", e); 
        	return null; 
        }
		
        
	}
	
}
